//原地复写0
public class duplicateZeros {
    public static void main(String[] args) {
        int[] arr = {1,0,2,3,0,4};
        duplicateZeros(arr);

    }
        public static void duplicateZeros(int[] arr) {
        int cur = 0,dest = -1,n = arr.length;
           while(cur < n) {
               if(arr[cur] == 0) dest += 2;
               else dest++;
               if(dest >= n-1) break;
               cur++;
            }
        if(dest == n) {
            arr[n - 1] = 0;
            cur--;
            dest -= 2;
        }
        while(cur >= 0) {
            if(arr[cur]  != 0) {
                arr[dest] = arr[cur];
                dest--;
                cur--;
            }
            else {
              arr[dest--] = 0;
              arr[dest--] = 0;
              cur--;
            }
        }
            for (int i = 0; i < n; i++) {
                System.out.println(arr[i]);
            }

    }
}
